package main

import (
	"encoding/json"
	"fmt"
	"github.com/BurntSushi/toml"
	"gopkg.in/yaml.v2"
	"log"
	"os"
)

type Config struct {
	AppName string   `toml:"APP_NAME" json:"app_name" yaml:"App_Name"`
	Env     []string `yaml:"Env"`
	App     *App     `yaml:"App"`
	Mysql   *Mysql   `yaml:"Mysql"`
}

type App struct {
	Path string `yaml:"Path"`
}

type Mysql struct {
	Host     string `yaml:"Host"`
	Name     string `yaml:"Name"`
	Password string `yaml:"Password"`
	Port     int16  `yaml:"Port"`
}

func main() {

	// 加载toml配置
	loadToml()

	// 加载json配置
	loadJson()

	// 加载yaml配置
	loadYaml()
}

func loadToml() {
	var c Config
	if _, err := toml.DecodeFile("./config.toml", &c); err != nil {
		log.Fatal(err)
		return
	}
	fmt.Printf("%s, %v, %v, %v\n", c.AppName, c.Env, c.App, c.Mysql)
}

func loadJson() {
	file, _ := os.Open("./config.json")
	defer file.Close()
	decoder := json.NewDecoder(file)

	var c Config
	if err := decoder.Decode(&c); err != nil {
		log.Fatal(err)
		return
	}
	fmt.Printf("%s, %v, %v, %v\n", c.AppName, c.Env, c.App, c.Mysql)
}

func loadYaml() {
	file, err := os.ReadFile("./config.yaml")
	if err != nil {
		log.Fatal(err)
		return
	}
	var c Config
	if err = yaml.Unmarshal(file, &c); err != nil {
		log.Fatal(err)
		return
	}
	fmt.Printf("%s, %v, %v, %v\n", c.AppName, c.Env, c.App, c.Mysql)
}
